package mall.kotlin.com.kotlinlearn.part

import mall.kotlin.com.kotlinlearn.part.partthree.Overload.Overloads
import mall.kotlin.com.kotlinlearn.test.minsOf
import org.junit.Test


class 第三章面向对象 {
    @Test
    fun Test1() {
        val computer = Computer()
        val usbMouse = LogitechMouser("罗技鼠标 ")
        computer.addInputDevice(usbMouse)
    }


    @Test
    fun Test2() {
        val person: Person = MaNong(23)
        person.work()
        println(person.age)

        val person2: Person = Doctor(24)
        person2.work()
        println(person2.age)
    }

    @Test
    fun Test3() {
        val driver = CarDriver()
        val writer = PPTWriter()
        val seniorManager = SeniorManager(driver, writer)
        seniorManager.drive()
        seniorManager.write()


        println(D(3).x())
        println(D(-10).x())
        println(D(-110).x())
        println(D(-10000).x())
    }

    @Test
    fun Test4() {
        val min = minOf(1, 2)//包级函数
        val mins = minsOf(1, 2)//自己定义的包级函数

        val latitude = Latitude.ofDouble(3.0)
        val latitude2 = Latitude.ofLatitude(latitude)

        println(Latitude.TAG)
    }

    //方法重载和默认参数
    @Test
    fun Test5() {
        val overloads = Overloads()
        overloads.a(3)

        val integerList = ArrayList<Int>()
        integerList.add(13)
        integerList.add(2)
        integerList.add(3)
        integerList.add(23)
        integerList.add(5)
        integerList.add(15)
        integerList.add(50)
        integerList.add(500)
        println(integerList)

        integerList.removeAt(1)
        integerList.remove(5)
        println(integerList)
    }


}

//抽象类和接口(接口反应的是事物能力)   <联想><笔记本>电脑
//中心词前面的修饰词就是接口所要实现的能力
interface InputDevice {
    fun input(event: Any)
}

interface USBInputDevice : InputDevice

interface BLEInputDevice : InputDevice

abstract class USBMouse(val name: String) : USBInputDevice {
    override fun input(event: Any) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun toString(): String {
        return name
    }
}

class LogitechMouser(val mouserName: String) : USBMouse(mouserName) {

}

class Computer {
    private fun addUSBInputDevice(inputDevice: InputDevice) {
        //插入输入设备
        println("add usb input device:$inputDevice")
    }

    private fun addBLEInputDevice(inputDevice: InputDevice) {
        //插入输入设备
        println("add ble input device:$inputDevice")
    }

    fun addInputDevice(inputDevice: InputDevice) {
        //插入输入设备
        when (inputDevice) {
            is BLEInputDevice -> {
                addBLEInputDevice(inputDevice)
            }
            is USBInputDevice -> {
                addUSBInputDevice(inputDevice)
            }
            else -> {
                throw IllegalArgumentException("不支持的设备类型")
            }
        }

    }
}


//子承父业
abstract class Person(open val age: Int) {
    abstract fun work()
}

class MaNong(age: Int) : Person(age) {

    override val age: Int
        get() = 0

    override fun work() {
        println("我是码农，我在写代码")
    }
}

class Doctor(age: Int) : Person(age) {
    override fun work() {
        println("我是医生，我在给病人看病")
    }
}


//接口代理
class Manager : Driver, Writer {
    override fun write() {

    }

    override fun drive() {

    }
}

class SeniorManager(val driver: Driver, val writer: Writer) : Driver by driver, Writer by writer

class CarDriver : Driver {
    override fun drive() {
        println("开车呢")
    }
}

class PPTWriter : Writer {
    override fun write() {
        println("做PPT呢")
    }

}

interface Driver {
    fun drive()
}

interface Writer {
    fun write()
}


//解决冲突 前提条件函数名一样、返回值一样
abstract class A {
    open fun x(): Int = 5
}

interface B {
    fun x(): Int = 1
}

interface C {
    fun x(): Int = 0
}

class D(var y: Int = 0) : A(), B, C {

    override fun x(): Int {
        println("call x(): Int in D")
        if (y > 0) {
            return y
        } else if (y < -200) {
            return super<C>.x()
        } else if (y < -100) {
            return super<B>.x()
        } else {
            return super<A>.x()
        }
    }
}


//单例模式
class Drivers

interface OnExternalDriverMountListener {
    fun onMount(driver: Drivers)

    fun onUnmount(driver: Drivers)
}

abstract class Player

object MusicPlayer : Player(), OnExternalDriverMountListener {
    override fun onMount(driver: Drivers) {

    }

    override fun onUnmount(driver: Drivers) {

    }

    val state: Int = 0

    fun play(url: String) {

    }

    fun stop() {

    }
}


//伴生对象和静态成员
class Latitude private constructor(val value: Double) {
    //这个类的伴生对象   相当于java中的静态方法
    companion object {
        @JvmStatic
        fun ofDouble(double: Double): Latitude {
            return Latitude(double)
        }

        fun ofLatitude(latitude: Latitude): Latitude {
            return Latitude(latitude.value)
        }

        @JvmField
        val TAG: String = "Latitude"
    }
}


//java中的静态内部类式实现
/**
 *
public class SingletonDemo {
private static class SingletonHolder{
private static SingletonDemo instance=new SingletonDemo();
}
private SingletonDemo(){
System.out.println("Singleton has loaded");
}
public static SingletonDemo getInstance(){
return SingletonHolder.instance;
}
}
 */

//kotlin中的静态内部类式实现
class SingletonDemo private constructor() {
    companion object {
        val instance = SingletonHolder.holder
    }

    private object SingletonHolder {
        val holder = SingletonDemo()
    }
}
